#include <iostream>
#include <string>
#include <deque>

using namespace std;

string s;
int count [10001][26];
int k;
deque<int> a;

int main()
{
	int ntest;
	cin >> ntest;

	while (0 < ntest--)
	{
		cin >> k;
		a.clear();
		a.push_back(0);		
		for (int i = 0; i < k; i++) 
		{
			int x;
			cin >> x;
			a.push_back(x);
		}
		cin >> s;
		a.push_front(s.size());
		s = '#' + s;

		for (int j = 0; j < 26; j++) count[0][j] = 0;
		for (int i = 1; i < (int) s.size(); i++)
		{
			for (int j = 0; j < 26; j++) count[i][j] = count[i-1][j];
			count[i][s[i] - 'a']++;
		}

		int res = 0;
		for (int i = 2; i < (int) a.size(); i++)
		{
			int maxp = -1, minp = 10000000;
			for (int j = i-1; j >= 0; j--)
			{
				if (a[j] < a[i] && maxp < a[j]) maxp = a[j];
				if (a[j] > a[i] && minp > a[j]) minp = a[j];
			}

			int left, right;
			for (int k = 0; k < 26; k++)
			{
				left = count[a[i]][k] - count[maxp][k];
				right = count[minp][k] - count[a[i]][k];

				if ((left == 0 && right != 0) || (left != 0 && right == 0)) res++;
			}

		}
		cout << res << endl;
	}

	return 0;
}
